/**
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function McHandler1(msg)
{ 
    debug("McHandler1: msg.guid=" + msg.guid + " msg.cmd=" + msg.cmd);
}

function TestMc(name)
{
    debug("TestMc: name=" + name);
    this.mc = new Mc(name, TestMc.prototype.handler);
    this.mc.me = this;
    this.avar = "Hello, wink";
    var serverName = "TestAjaxServer1";
    var msg = McMgr.getMsg();
    msg.dstId = McMgr.lookup(serverName);
    if (msg.dstId < 0)
    {
        throw "TestMc: lookup of " + serverName + " failed" 
    }
    msg.guid = 0x789;
    msg.srcId = this.mc.mcId.id; 
    msg.cmd = McConst.mkRequest(0x789);
    msg.tag = 1;
    debug("TestMc: send to TestAjaxServer msg._id=" + msg._id);
    McMgr.sendMsg(msg);
}

TestMc.prototype.handler = function (msg)
{
    debug("TestMc: msg.guid=" + msg.guid + " msg.cmd=" + msg.cmd + " msg.tag=" + msg.tag);
    debug("TestMc: me.avar='" + this.me.avar + "'");
    if (msg.tag < 5) {
        msg = McMgr.getReplyMsg(msg);
        msg.cmd = McConst.mkRequest(0x789);
        msg.tag += 1;
        McMgr.sendMsg(msg);
    }
}
    
function test()
{
	debug("test");
	
	/* Spot check some McConst's */
	if (McConst.MC_CMD_NOP != 0)
	   throw "Bad McConst.MC_CMD_NOP"
	if (McConst.MC_CMD_STARTED != 0x10)
	   throw "Bad McConst.MC_CMD_STARTED"
	if (McConst.MCMASTER_CMD_REGISTER_DEV != 0x11)
	   throw "Bad McConst.MCMASTER_CMD_REGISTER_DEV"
	if (McConst.STATUS_ERR != 1)
	   throw "Bad McConst.STATUS_ERR"
	   
	/* Test we can serialize and deserialize a Msg */
	debug("t1\n");
	m1 = McMgr.getMsg();
	m1.arg1 = 1;
	ms = m1.toJSONString();
	o = Deserializer.deserialize(ms);
	if (o.arg1 != 1) {
	    throw "Bad deserialization";
	}
	
	/* Test we can serialize/deserialize a Msg in msg.obj */
	debug("t2\n");
	m1.obj = McMgr.getMsg();
	m1.obj.arg1 = 2;
	ms = m1.toJSONString();
	o = Deserializer.deserialize(ms);
	if (o.arg1 != 1) {
	    throw "Bad deserialization";
	}
	if (o.obj.arg1 != 2) {
	    throw "Bad deserialization";
	}
	
	/* Simple mc test, send a message*/
    debug("t3\n");
    name = "mc" + McMgr.amp.sessionId;
    new Mc(name, McHandler1);
    msg = McMgr.getMsg();
    msg.dstId = McMgr.lookup(name);
    msg.guid = 0x123;
    msg.cmd = McConst.mkCmd(0x123);
    debug("msg.dstId=" + msg.dstId);
    McMgr.sendMsg(msg);
    
    debug("t4\n");
    name = "TestMc" + McMgr.amp.sessionId;
    new TestMc(name);
    msg = McMgr.getMsg();
    msg.dstId = McMgr.lookup(name);
    msg.guid = 0x456;
    msg.cmd = McConst.mkCmd(0x456);
    debug("msg.dstId=" + msg.dstId);
    McMgr.sendMsg(msg);
    
    debug("test done");
}
